﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;

namespace BookFinder
{
    [Serializable]
    public class BooksCollection : CollectionBase
    {

        public int Add(Book value)
        {
            return base.List.Add(value);
        }

        public void sortBy(String columnName)
        {

            System.Collections.IComparer sorter = null;
            switch (columnName)
            {
                case "ISBN":
                    sorter = new ISBNSortHelper();
                    break;

                case "Author":
                    sorter = new AuthorSortHelper();
                    break;

                case "Price":
                    sorter = new PriceSortHelper();
                    break;

                case "Title":
                    sorter = new TitleSortHelper();
                    break;

                default:
                    break;
            }
            if (sorter != null)
            {
                InnerList.Sort(sorter);
            }

        }

        public Book this[int index]
        {
            get { return (Book)base.List[index]; }
            set { base.List[index] = value; }
        }

        private class ISBNSortHelper : System.Collections.IComparer
        {
            public int Compare(object x, object y)
            {
                if (x == y) return 0;
                if (x == null) return -1;
                if (y == null) return 1;
                Book p1 = (Book)x;
                Book p2 = (Book)y;

                if (p1.ISBN == null) return 1;
                if (p2.ISBN == null) return -1;
                if (p1.ISBN == p2.ISBN) return 0;

                return p1.ISBN.CompareTo(p2.ISBN);
            }
        }

        private class AuthorSortHelper : System.Collections.IComparer
        {
            public int Compare(object x, object y)
            {
                if (x == y) return 0;
                if (x == null) return -1;
                if (y == null) return 1;
                Book p1 = (Book)x;
                Book p2 = (Book)y;                
                
                if (p1.Author == null) return 1;
                if (p2.Author == null) return -1;
                if (p1.Author ==  p2.Author) return 0;

                return p1.Author.CompareTo(p2.Author);
            }
        }

        private class PriceSortHelper : System.Collections.IComparer
        {
            public int Compare(object x, object y)
            {
                if (x == y) return 0;
                if (x == null) return -1;
                if (y == null) return 1;
                Book p1 = (Book)x;
                Book p2 = (Book)y;

                if (p1.Price == p2.Price) return 0;

                return p1.Price.CompareTo(p2.Price);
            }
        }

        private class TitleSortHelper : System.Collections.IComparer
        {
            public int Compare(object x, object y)
            {
                if (x == y) return 0;
                if (x == null) return -1;
                if (y == null) return 1;
                Book p1 = (Book)x;
                Book p2 = (Book)y;

                if (p1.Title == null) return 1;
                if (p2.Title == null) return -1;
                if (p1.Title == p2.Title) return 0;

                return p1.Title.CompareTo(p2.Title);
            }
        }
    }
}